#include "handler.h"
#include "../tlv/tlv.h"
#include "user_login/login/login.h"
#include "user_login/register/register.h"
#include "user_login/delete_user/userdel.h"
#include "token/token.h"
#include "directory/cmd.h"
#include "timeoutManager/timeoutManager.h"

#include <string.h>
#include <strings.h>

system_error_code_t handle_login_request(user_session_t *user_session, tlv_msg_t *msg)
{
    // 参数检查
    if (user_session == NULL || msg == NULL)
    {
        return SYSTEM_ERR_INVALID_ARGUMENT;
    }

    // 更新用户活跃时间
    timeout_manager_update_user_activity(&g_timeout_manager, user_session->netfd);

    // 提取用户名
    memcpy(user_session->username, msg->value, sizeof(user_session->username) - 1);
    // 调用登录逻辑，比如查询数据库，生成盐值等
    int ret = do_login_first(user_session);
    printf("do_login_first returned %d\n", ret);
    // 错误检测
    if (ret != SYSTEM_ERR_OK)
    {
        return ret;
    }
    // 登录逻辑成功，更新会话状态
    user_session->state = CONN_STATE_AWAITING_PASSWORD;

    return SYSTEM_ERR_OK;
}

system_error_code_t handle_password_request(user_session_t *user_session, tlv_msg_t *msg)
{
    // 参数检查
    if (user_session == NULL || msg == NULL)
    {
        return SYSTEM_ERR_INVALID_ARGUMENT;
    }

    // 更新用户活跃时间
    timeout_manager_update_user_activity(&g_timeout_manager, user_session->netfd);

    // 提取密码（密文）
    char received_password[256];
    memcpy(received_password, msg->value, sizeof(received_password) - 1);

    // 验证密码逻辑
    int ret = do_login_second(user_session, received_password);
    if (ret != SYSTEM_ERR_OK)
    {
        return ret;
    }
    // 密码验证成功，更新会话状态
    user_session->state = CONN_STATE_AUTHENTICATED;

    //将用户名和session关联起来
    session_manager_register_username(&g_session_manager, user_session->username, user_session);

    // 向客户端发送 token
    char *token = NULL;
    size_t length = 0;
    ret = generate_jwt_token(user_session->username, &token, &length);
    if (ret != SYSTEM_ERR_OK)
    {
        return ret;
    }
    // 发送 token 给客户端
    tlv_msg_t response_msg;
    bzero(&response_msg, sizeof(tlv_msg_t));
    ret = tlv_create(&response_msg, TOKEN, length, token);
    if (ret != SYSTEM_ERR_OK)
    {
        free(token);
        return ret;
    }
    ret = tlv_send(user_session->netfd, &response_msg);
    free(token);
    if (ret != SYSTEM_ERR_OK)
    {
        return ret;
    }

    return SYSTEM_ERR_OK;
}

system_error_code_t handle_register_request(user_session_t *user_session)
{
    // 参数检查
    if (user_session == NULL)
    {
        return SYSTEM_ERR_INVALID_ARGUMENT;
    }

    // 更新用户活跃时间
    timeout_manager_update_user_activity(&g_timeout_manager, user_session->netfd);

    // 调用注册逻辑
    int ret = do_register(user_session);
    if (ret != SYSTEM_ERR_OK)
    {
        return ret;
    }

    user_session->state = CONN_STATE_INITIAL;

    return SYSTEM_ERR_OK;
}

system_error_code_t handle_delete_request(user_session_t *user_session)
{
    // 参数检查
    if (user_session == NULL)
    {
        return SYSTEM_ERR_INVALID_ARGUMENT;
    }

    // 更新用户活跃时间
    timeout_manager_update_user_activity(&g_timeout_manager, user_session->netfd);

    // 调用注销逻辑
    int ret = do_delete(user_session);
    if (ret != SYSTEM_ERR_OK)
    {
        return ret;
    }

    return SYSTEM_ERR_OK;
}

system_error_code_t handle_cd_request(user_session_t *user_session, tlv_msg_t *msg)
{
    // 参数检查
    if (user_session == NULL || msg == NULL)
    {
        return SYSTEM_ERR_INVALID_ARGUMENT;
    }

    // 更新用户活跃时间
    timeout_manager_update_user_activity(&g_timeout_manager, user_session->netfd);

    // 调用 cd 函数
    int ret = cdFunc(user_session, msg->value);
    if (ret != 0)
    {
        // TODO: 错误处理
        return ret;
    }

    return SYSTEM_ERR_OK;
}

system_error_code_t handle_ls_request(user_session_t *user_session)
{
    // 参数检查
    if (user_session == NULL)
    {
        return SYSTEM_ERR_INVALID_ARGUMENT;
    }

    // 更新用户活跃时间
    timeout_manager_update_user_activity(&g_timeout_manager, user_session->netfd);

    // 调用 ls 函数
    int ret = lsFunc(user_session);
    if (ret != 0)
    {
        // TODO: 错误处理
        return ret;
    }

    return SYSTEM_ERR_OK;
}

system_error_code_t handle_pwd_request(user_session_t *user_session)
{
    // 参数检查
    if (user_session == NULL)
    {
        return SYSTEM_ERR_INVALID_ARGUMENT;
    }

    // 更新用户活跃时间
    timeout_manager_update_user_activity(&g_timeout_manager, user_session->netfd);

    // 调用 pwd 函数
    int ret = pwdFunc(user_session);
    if (ret != 0)
    {
        // TODO: 错误处理
        return ret;
    }

    return SYSTEM_ERR_OK;
}

system_error_code_t handle_mkdir_request(user_session_t *user_session, tlv_msg_t *msg)
{
    // 参数检查
    if (user_session == NULL || msg == NULL)
    {
        return SYSTEM_ERR_INVALID_ARGUMENT;
    }

    // 更新用户活跃时间
    timeout_manager_update_user_activity(&g_timeout_manager, user_session->netfd);

    // 调用 mkdir 函数
    int ret = mkdirFunc(user_session, msg->value);
    if (ret != 0)
    {
        // TODO: 错误处理
        return ret;
    }

    return SYSTEM_ERR_OK;
}

system_error_code_t handle_rmdir_request(user_session_t *user_session, tlv_msg_t *msg)
{
    // 参数检查
    if (user_session == NULL || msg == NULL)
    {
        return SYSTEM_ERR_INVALID_ARGUMENT;
    }

    // 更新用户活跃时间
    timeout_manager_update_user_activity(&g_timeout_manager, user_session->netfd);

    // 调用 rmdir 函数
    int ret = rmdirFunc(user_session, msg->value);
    if (ret != 0)
    {
        // TODO: 错误处理
        return ret;
    }

    return SYSTEM_ERR_OK;
}

system_error_code_t handle_short_command_request(user_session_t *user_session, tlv_msg_t *msg)
{
    // 参数检查
    if (user_session == NULL || msg == NULL)
    {
        return SYSTEM_ERR_INVALID_ARGUMENT;
    }

    // TODO: 处理短命令逻辑
    int ret = 0;
    switch (msg->hdr.type)
    {
    case CMD_CD:
        printf("Handling cd request\n");
        ret = handle_cd_request(user_session, msg);
        printf("Handle cd request ret = %d\n", ret);
        break;
    case CMD_LS:
        printf("Handling ls request\n");
        ret = handle_ls_request(user_session);
        printf("Handle ls request ret = %d\n", ret);
        break;
    case CMD_PWD:
        printf("Handling pwd request\n");
        ret = handle_pwd_request(user_session);
        printf("Handle pwd request ret = %d\n", ret);
        break;
    case CMD_MKDIR:
        printf("Handling mkdir request\n");
        ret = handle_mkdir_request(user_session, msg);
        printf("Handle mkdir request ret = %d\n", ret);
        break;
    case CMD_RMDIR:
        printf("Handling rmdir request\n");
        ret = handle_rmdir_request(user_session, msg);
        printf("Handle rmdir request ret = %d\n", ret);
        break;
    case DO_DELETE:
        // 处理删除用户请求
        printf("Handling delete user request\n");
        ret = handle_delete_request(user_session);
        printf("Handle delete user request ret = %d\n", ret);
        break;
    }

    // 目前假设处理成功
    return SYSTEM_ERR_OK;
}